/**
 * Created by yy on 2017/8/4.
 * 请求 确认上岗名单数据
 */
$(function(){
    var url1 = location.search;
    if (url1.indexOf("?") != -1) {
        var id = url1.substr(4);}
    var args={"workMessageId":id};
    var url2 = server + '/server/business/leaderRest/leaderMessage.json';
    $.post(url2,args, function(data){

        var ctx2 = template('./management-job-details-2-tpl', data.data);
        var ctx3 = template('./management-job-details-3-tpl', data.data);
        var ctx4 = template('./management-job-details-4-tpl', data.data);

        $("#details-2").html(ctx2);
        $("#details-3").html(ctx3);
        $("#details-4").html(ctx4);
        member_change();
    });
});




function member_change(){
    //盒子上下箭头切换
    $('.slideToggle').on('click',function(){
        $(this).parents('.job-details-content').find('table tbody tr:nth-child(n+3) td').slideToggle(50);
    });

    var url0 = location.search;
    if (url0.indexOf("?") != -1) {
        var messageId = url0.substr(4);
    }

    //删除报名成员且追加到移出人员里面
    $('.delete-user').on('click',function(){

        var userId = $(this).parents('td').parent().find("td:first-child").text();
        var url1 = server + '/server/business/leaderRest/deleteFromWorker.json';
        var args1 = {"userId":userId,"messageId":messageId};
        $.post(url1, args1, function (data) {
            if(data.code == 20000){
                alert("人员调动成功");
            }
        });

        var sing_ups= $(this).parents('tr');
        var remove_user=$('.remove-user');
        $(this).removeClass('delete-user').addClass('add-user').on('click',function(){
            $(this).parents('tr').appendTo($('.sign-up-user'));
        });
        $(this).find('i').attr('class','ace-icon fa fa-plus bigger-120');
        sing_ups.appendTo(remove_user);
    });
    //增加成员
    $('.add-user').on('click',function(){

        var theBox = $(this).parents('.box').find('span').text();
        if(theBox=="备用人员"){
            var userId2 = $(this).parents('td').parent().find("td:first-child").text();

            var url2 = server + '/server/business/leaderRest/addSpareToWorkerById.json';
            var args2 = {"userId":userId2,"messageId":messageId};
            $.post(url2, args2, function (data) {
                if(data.code == 20000){
                    alert("人员调动成功");
                }
            });
        }
        else{
            var userId3 = $(this).parents('td').parent().find("td:first-child").text();

            var url3 = server + '/server/business/leaderRest/recoverWorker.json';
            var args3 = {"userId":userId3,"messageId":messageId};
            $.post(url3, args3, function (data) {
                if(data.code == 20000){
                    alert("人员调动成功");
                }
            });
        }

        var user=$(this).parents('tr');
        $(this).removeClass('add-user').addClass('delete-user').on('click',function(){
            $(this).parents('tr').remove();
        });
        $(this).find('i').attr('class','ace-icon fa fa-remove bigger-120');
        var signUp= $('.sign-up-user');
        user.appendTo(signUp);
    });
}

